package com.comast.eep.bridge.jms;

import java.io.IOException;
import java.util.Map;

import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import weblogic.jms.extensions.WLMessageProducer;

import com.comcast.eep.bridge.common.EEPBridgeUtils;
import com.comcast.eep.bridge.model.Consumer;

public class JMSSenderImpl implements JMSSender {

	private static final Log logger = LogFactory.getLog(JMSSenderImpl.class);
	private static final String Y = "Y";
	private static final String N = "N";
	
	private QueueConnectionFactory qcf = null;
	private QueueConnection qc = null;
	private QueueSession qsess = null;
	private Queue queue = null;
	private WLMessageProducer qsndr = null;
	private TextMessage message = null;
	private BytesMessage bMsg = null;
	byte[] rawBytes = null;

	public String sendMessage(Message msg, Map<String, Consumer> cons) {
		Consumer consumer = null;
		StringBuffer sb = new StringBuffer();
		try {

			bMsg = (BytesMessage) msg;
			rawBytes = EEPBridgeUtils.getRawData(bMsg);

			Context ctx = new InitialContext();
			String account = msg.getStringProperty("ACCOUNT_ID");

			if (logger.isDebugEnabled()) {
				logger.debug("Got Initial Context: " + ctx + "Account: "
						+ account);
			}

			rawBytes = EEPBridgeUtils.decompressData(rawBytes);
			String tMsg = new String(rawBytes);

			if (logger.isDebugEnabled()) {
				logger.debug("Text:" + tMsg.substring(0, 50));
			}

			for (String key : cons.keySet()) {

				consumer = (Consumer) cons.get(key);

				if (logger.isDebugEnabled()) {
					logger.debug("ConnectionFactory:"
							+ consumer.getConnFactory() + "Queue_Name:"
							+ consumer.getDestQueue());
				}

				// Create Queue Connection Factory
				qcf = (QueueConnectionFactory) ctx.lookup(consumer
						.getConnFactory());

				// Create Queue Connection
				qc = qcf.createQueueConnection();

				// Create Queue Session
				qsess = qc.createQueueSession(false, 0);

				// Lookup Queue
				queue = (Queue) ctx.lookup(consumer.getDestQueue());

				// Create Queue Sender
				qsndr = (WLMessageProducer) qsess.createSender(queue);
				qsndr.setUnitOfOrder(account);

				if (consumer.getFiltered().equalsIgnoreCase(N)) {
					sb.append(key);
					if (consumer.getCompressed().equalsIgnoreCase(Y)) {
						qsndr.send(msg);
					} else {
						// Create Text Message
						message = qsess.createTextMessage(tMsg);
						
						message.setStringProperty("EVENTSOURCE_KEY", 			msg.getStringProperty("EVENTSOURCE_KEY"));
						message.setStringProperty("ACCOUNT_ID", 				msg.getStringProperty("ACCOUNT_ID"));
						message.setStringProperty("BUSINESS_UNIT",				msg.getStringProperty("BUSINESS_UNIT"));
						message.setStringProperty("EVENT_TRIGGER", 				msg.getStringProperty("EVENT_TRIGGER"));
						message.setStringProperty("EVENT_SOURCE", 				msg.getStringProperty("EVENT_SOURCE"));
						
						message.setIntProperty("ACCOUNT_CHANGED_ACTIVITY", 		msg.getIntProperty("ACCOUNT_CHANGED_ACTIVITY"));
						message.setIntProperty("LOCATION_CHANGED_ACTIVITY", 	msg.getIntProperty("LOCATION_CHANGED_ACTIVITY"));
						message.setIntProperty("ORDER_CHANGED_ACTIVITY", 		msg.getIntProperty("ORDER_CHANGED_ACTIVITY"));
						message.setIntProperty("DEVICE_CHANGED_ACTIVITY", 		msg.getIntProperty("DEVICE_CHANGED_ACTIVITY"));
						message.setIntProperty("SERVICE_CHANGED_ACTIVITY",  	msg.getIntProperty("SERVICE_CHANGED_ACTIVITY"));
						
						message.setLongProperty("BUSINESS_CUSTOMER_TYPE", 		msg.getLongProperty("BUSINESS_CUSTOMER_TYPE"));
						message.setIntProperty("LOB", 							msg.getIntProperty("LOB"));
						
						qsndr.send(message);
					}

					if (logger.isDebugEnabled()) {
						logger.debug("Queued_For_" + key + "_ConnFactory:"
								+ consumer.getConnFactory() + "_Queue:"
								+ consumer.getDestQueue());
					}
				}

			}

		} catch (NamingException e) {
			logger.error(
					"NamingException_ConnectionFactory:"
							+ consumer.getConnFactory() + "_Queue_Name:"
							+ consumer.getDestQueue(), e);
		} catch (JMSException e) {
			logger.error(
					"JMSException_ConnectionFactory:"
							+ consumer.getConnFactory() + "_Queue_Name:"
							+ consumer.getDestQueue(), e);
		} catch (IOException e) {
			logger.error(
					"IOException_ConnectionFactory:"
							+ consumer.getConnFactory() + "_Queue_Name:"
							+ consumer.getDestQueue(), e);
		}

		return sb.toString();
	}

}
